package com.Ugent.Minerva;


import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;


public class Announcements extends ExpandableListActivity 
{
    TextView gebruiker;
    TextView aankondiging;
    private Student s1 = null;
    ExpandableListAdapter adapter;

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.announcement);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        aankondiging = (TextView) findViewById(R.id.aankondiging);
        s1 = (Student) getIntent().getSerializableExtra("student");
        gebruiker = (TextView) findViewById(R.id.gebruiker);
        gebruiker.setText(s1.getName());

        // Set up our adapter for expandable listview
        adapter = new AnnouncementAdapter();
        setListAdapter(adapter);
        registerForContextMenu(getExpandableListView());
            
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();

        String title = ((TextView) info.targetView).getText().toString();

        int type = ExpandableListView.getPackedPositionType(info.packedPosition);
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
            int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); 
            Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
                    Toast.LENGTH_SHORT).show();
            return true;
        } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
            Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }
    
    public void clickHandler(View v){       
        
        startActivity(new Intent(getApplicationContext(), Account.class));

    }
    
    //expandable listadapter
    public class AnnouncementAdapter extends BaseExpandableListAdapter {

      //groups = titles
	  private String [] groups = toGroups(); 
	  public String [] toGroups(){
		  @SuppressWarnings("unchecked")
			ArrayList<Announcement> announcements = (ArrayList<Announcement>) getIntent().getSerializableExtra("announcements");
			ArrayList<String> title = new ArrayList<String>();

	    	for (int i = 0; i<announcements.size();i++){
	    		title.add(announcements.get(i).getTitle());	
	    	}
	    	
	    	String [] groups = title.toArray(new String[title.size()])	;
    		return groups;
	  }
	  
	  //children = description/announcements
	  private String [][] children = toChildren();
	  public String [][] toChildren(){
		  @SuppressWarnings("unchecked")
			ArrayList<Announcement> announcements = (ArrayList<Announcement>) getIntent().getSerializableExtra("announcements");
			ArrayList<String> title = new ArrayList<String>();
			ArrayList<String> announcement = new ArrayList<String>();
			
	    	for (int i = 0; i<announcements.size();i++){
	    		title.add(announcements.get(i).getTitle());
	    		announcement.add(announcements.get(i).getAnnouncement());	
	    	}
		  
	    	String [][] children = new String[title.size()][1];
	    	for (int i = 0; i<title.size(); i++){
	    		children[i][0]=announcement.get(i);
	    	}
	    	return children;
	  }
	  
      public Object getChild(int groupPosition, int childPosition) {
          return children[groupPosition][childPosition];
      }

      public long getChildId(int groupPosition, int childPosition) {
          return childPosition;
      }

      public int getChildrenCount(int groupPosition) {
          return children[groupPosition].length;
      }

      public TextView getGenericView() {
          // Layout parameters for the ExpandableListView
          AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 64);

          TextView textView = new TextView(Announcements.this);
          textView.setLayoutParams(lp);
          // Center the text vertically
          textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
          // Set the text starting position
          textView.setPadding(36, 0, 0, 0);
          return textView;
      }

      public Object getGroup(int groupPosition) {
          return groups[groupPosition];
      }

      public int getGroupCount() {
          return groups.length;
      }

      public long getGroupId(int groupPosition) {
          return groupPosition;
      }

      public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
    	  View groupRow = getLayoutInflater().inflate(R.layout.group_row, null);
    	  TextView textView = (TextView) groupRow.findViewById(R.id.text_group);
    	  textView.setText(getGroup(groupPosition).toString());
    	  return groupRow;
      }

      public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    	  View childRow = getLayoutInflater().inflate(R.layout.child_row, null);
    	  TextView textView = (TextView) childRow.findViewById(R.id.text_child);
    	  textView.setText(Html.fromHtml(getChild(groupPosition, childPosition).toString()));
    	  return childRow;
      }

      public boolean isChildSelectable(int groupPosition, int childPosition) {
          return true;
      }

      public boolean hasStableIds() {
          return true;
      }

   }
}